home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / intuisup.lha / Intuisup / source.lha / Language / language.c < prev    next >
C/C++ Source or Header  |  1992-06-04  |  6KB  |  242 lines

  1. /* $Revision Header *** Header built automatically - do not edit! ***********
  2.  *
  3.  *    (C) Copyright 1991 by Torsten Jürgeleit
  4.  *
  5.  *    Name .....: language.c
  6.  *    Created ..: Thursday 19-Dec-91 20:21:06
  7.  *    Revision .: 0
  8.  *
  9.  *    Date        Author                 Comment
  10.  *    =========   ====================   ====================
  11.  *    04-Jun-92   Torsten Jürgeleit      use raster for auto requester
  12.  *    11-Apr-92   Torsten Jürgeleit      larger read and line buffer
  13.  *    19-Dec-91   Torsten Jürgeleit      Created this file!
  14.  *
  15.  ****************************************************************************
  16.  *
  17.  *    Support routines for using texts of different languages for ISUP
  18.  *    objects
  19.  *
  20.  * $Revision Header ********************************************************/
  21.  
  22.     /* Includes */
  23.  
  24. #include <exec/types.h>
  25. #include <exec/memory.h>
  26. #include <libraries/dos.h>
  27. #ifdef AZTEC_C
  28. #include <functions.h>   /* needed for Aztec C - prototypes and pragmas for all Amiga system functions */
  29. #endif
  30. #include <libraries/memwatch.h>   /* header file for memory debug link library (Fish 240) - AFTER functions.h */
  31. #include <string.h>
  32. #include "/requester/requester.h"
  33. #include "/files/files.h"
  34. #include "language.h"
  35.  
  36.     /* Defines */
  37.  
  38. #define LANGUAGE_FILE_READ_BUFFER_SIZE    10000
  39. #define LANGUAGE_FILE_LINE_BUFFER_SIZE    1000
  40. #define LANGUAGE_FILE_FLAGS        (TEXT_FILE_FLAG_TRIM_LINE | TEXT_FILE_FLAG_SKIP_COMMENTS | TEXT_FILE_FLAG_SKIP_EMPTY_LINES | TEXT_FILE_FLAG_LINE_CONTINUATION)
  41.  
  42. #define LANGUAGE_ERROR_REQ_TITLE    " Build Language Text Array "
  43. #define LANGUAGE_ERROR_REQ_POS_TEXT    "_Continue"
  44. #define LANGUAGE_ERROR_REQ_FLAGS    (AUTO_REQ_FLAG_BACK_FILL | AUTO_REQ_FLAG_TEXT_CENTER | AUTO_REQ_FLAG_TEXT_COLOR2 | AUTO_REQ_FLAG_HOTKEY | AUTO_REQ_FLAG_DRAW_RASTER)
  45.  
  46.     /* Statics */
  47.  
  48. STATIC BYTE invalid_msg[] = "Invalid";
  49.  
  50.     /* Build language text array by parsing given text file */
  51.  
  52.    BYTE **
  53. build_language_text_array(BYTE *name, USHORT entries)
  54. {
  55.    BYTE *error = NULL;
  56.    LONG arg;
  57.  
  58.    if (name && entries) {
  59.       struct FileData  *fd;
  60.       BYTE error_line[80];
  61.  
  62.       if (!(fd = open_text_file(name, LANGUAGE_FILE_READ_BUFFER_SIZE,
  63.             LANGUAGE_FILE_LINE_BUFFER_SIZE, LANGUAGE_FILE_FLAGS))) {
  64.      error = "Can't open language text file `%s'";
  65.      arg   = (LONG)name;
  66.       } else {
  67.          BYTE **text_array;
  68.  
  69.      if (!(text_array = AllocMem((LONG)(sizeof(BYTE *) * (entries + 1)),
  70.                               (LONG)MEMF_PUBLIC))) {
  71.         error = "Out of memory";
  72.      } else {
  73.             BYTE   **array = text_array;
  74.         USHORT count;
  75.  
  76.         /* Init text array entry count */
  77.         *array++ = (BYTE *)(LONG)entries;
  78.  
  79.         /* Read text array entries from text file */
  80.         count = entries;
  81.         while (count && error == NULL) {
  82.            BYTE   *text;
  83.            USHORT len;
  84.  
  85.            switch (read_text_line(fd)) {
  86.           case TEXT_FILE_STATUS_NORMAL :
  87.              text = fd->fd_Line;
  88.              len  = fd->fd_LineLen;
  89.              if (len < 2) {
  90.             error = "Line %ld invalid";
  91.             arg   = fd->fd_LineNum;
  92.              } else {
  93.  
  94.             /* Strip double quotes */
  95.             if (*text++ != '"' || *(text + (len -= 2)) != '"') {
  96.                error = "Missing double quotes in line %ld";
  97.                arg   = fd->fd_LineNum;
  98.             } else {
  99.                BYTE *ptr;
  100.  
  101.                if (!(ptr = AllocMem((LONG)(len + 1),
  102.                               (LONG)MEMF_PUBLIC))) {
  103.                   error = "Out of memory";
  104.                } else {
  105.                   strncpy(ptr, text, (size_t)len);
  106.                   *(ptr + len) = '\0';
  107.                   *array++     = ptr;
  108.                   count--;
  109.                }
  110.                 }
  111.              }
  112.              break;
  113.  
  114.           case TEXT_FILE_STATUS_EOF :
  115.              error = "Too few entries (%ld)";
  116.              arg   = entries - count;
  117.              break;
  118.  
  119.           case TEXT_FILE_ERROR_LINE_TOO_LONG :
  120.              error = "Line %ld too long";
  121.              arg   = fd->fd_LineNum;
  122.              break;
  123.  
  124.           case TEXT_FILE_ERROR_NO_COMMENT_END :
  125.              error = "No comment end in line %ld";
  126.              arg   = fd->fd_LineNum;
  127.              break;
  128.  
  129.           case TEXT_FILE_ERROR_READ_FAILED :
  130.              error = "Read failed";
  131.              break;
  132.            }
  133.         }
  134.         if (!error) {
  135.            close_text_file(fd);
  136.            return(text_array);
  137.         }
  138.         FreeMem(text_array, (LONG)(4 * (entries + 1)));
  139.      }
  140.      close_text_file(fd);
  141.       }
  142.  
  143.       /* Display auto request with error message */
  144.       sprintf(&error_line[0], error, arg);   /* private sprintf() !!! */
  145.       auto_request(NULL, LANGUAGE_ERROR_REQ_TITLE, &error_line[0],
  146.                   LANGUAGE_ERROR_REQ_POS_TEXT, NULL, 0L, 0L,
  147.                         LANGUAGE_ERROR_REQ_FLAGS, NULL);
  148.    }
  149.    return(NULL);
  150. }
  151.     /* Get text from given language text array */
  152.  
  153.    BYTE *
  154. get_language_text(BYTE *text, BYTE **text_array)
  155. {
  156.    BYTE *ptr = &invalid_msg[0];
  157.  
  158.    if (!text_array) {
  159.       if (text) {
  160.      ptr = text;
  161.       }
  162.    } else {
  163.       if ((ULONG)text > 0 && (ULONG)text <= (ULONG)*text_array) {
  164.      ptr = *(text_array + (ULONG)text);
  165.       }
  166.    }
  167.    return(ptr);
  168. }
  169.     /* Free given language text array */
  170.  
  171.    VOID
  172. free_language_text_array(BYTE **text_array)
  173. {
  174.    BYTE **array;
  175.  
  176.    if (array = text_array) {
  177.       USHORT entries;
  178.  
  179.       if (entries = (USHORT)(LONG)*array++) {
  180.      USHORT i;
  181.  
  182.      for (i = 0; i < entries; i++) {
  183.         BYTE *text = *array++;
  184.  
  185.         FreeMem(text, (LONG)(strlen(text) + 1));
  186.      }
  187.       }
  188.       FreeMem(text_array, (LONG)(sizeof(BYTE *) * (entries + 1)));
  189.    }
  190. }
  191.     /* Private sprintf() with RawDoFmt() from Exec */
  192.  
  193. #asm
  194. ;---------------------------------------------------------------------------
  195. ; Support macros
  196. ;---------------------------------------------------------------------------
  197.  
  198. PUSH    MACRO
  199.     movem.l    \1,-(sp)
  200.     ENDM
  201.  
  202. PULL    MACRO
  203.     movem.l    (sp)+,\1
  204.     ENDM
  205.  
  206. CALLSYS    MACRO
  207.     XREF    _LVO\1
  208.     jsr    _LVO\1(a6)
  209.     ENDM
  210.  
  211. ;---------------------------------------------------------------------------
  212. ; External definitions
  213. ;---------------------------------------------------------------------------
  214.  
  215.     XDEF    _sprintf
  216.  
  217. ;---------------------------------------------------------------------------
  218. ; LONG sprintf(BYTE *buffer, BYTE *format, ...)
  219. ;---------------------------------------------------------------------------
  220.  
  221. _sprintf:
  222.     PUSH    d7/a2-a6
  223.  
  224.     move.l    1*4+6*4(sp),a3        ; a3 := output buffer ptr
  225.     move.l    2*4+6*4(sp),a0        ; a0 := format string ptr
  226.     lea    3*4+6*4(sp),a1        ; a1 := data ptr
  227.     lea    put_char(pc),a2        ; a2 := put character routine
  228.     moveq    #0,d7            ; d7 := character counter
  229.     move.l    (4).w,a6        ; a6 := exec ptr
  230.     CALLSYS    RawDoFmt(a6)        ; do format
  231.     move.l    d7,d0            ; d0 := character counter
  232.  
  233.     PULL    d7/a2-a6
  234.     rts
  235.  
  236.     ; --- put character routine for RawDoFmt
  237. put_char:
  238.     move.b    d0,(a3)+        ; write character to output buffer
  239.     addq.l    #1,d7            ; increment character counter
  240.     rts
  241. #endasm
  242.